package com.itheima.service.impl;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.dto.UserOrderDto;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.vo.OrderVo;
import com.itheima.mapper.MemberDao;
import com.itheima.mapper.OrderDao;
import com.itheima.mapper.OrdersettingDao;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    private static final String CODE_KEY_PREFIX = "validate_code_";
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private OrdersettingDao ordersettingDao;

    /**
     * 分页查询预约
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<OrderVo> orders = orderDao.findPage(queryPageBean.getQueryString());
        PageInfo<OrderVo> pageInfo = new PageInfo<>(orders);
        PageResult pageResult = new PageResult(pageInfo.getTotal(),pageInfo.getList());

        return pageResult;
    }

    /**
     * 修改状态
     * @param order
     */
    @Override
    public void update(Order order) {
        orderDao.update(order);
    }

    /**
     * 根据id查询预约
     *
     * @param id
     * @return
     */
    @Override
    public List<OrderVo> findById(Integer id) {
        List<OrderVo> orders = orderDao.findById(id);
        return orders;
    }

    /**
     * 提交预约
     * @param orderDto
     */
    @Override
    public void submitOrder(UserOrderDto orderDto) {
        Member member = new Member();
        Order order = new Order();
        BeanUtils.copyProperties(orderDto, order);
        BeanUtils.copyProperties(orderDto, member);
        //检验验证码是否正确
        String redisCode =(String) redisTemplate.opsForValue().get(CODE_KEY_PREFIX + orderDto.getTelephone());
        if (!orderDto.getValidateCode().equals(redisCode)){
            throw new RuntimeException("验证码错误");
        }

        //预约日期的预约人数没有设置的话不能预约;预约日期是否已经约满，如果已经约满则无法预约。
        //根据日期查找预约设置
        OrderSetting orderSetting = ordersettingDao.getOrderSettingByDate(orderDto.getOrderDate());
        if (orderSetting == null){
            throw new RuntimeException("预约日期没有设置");
        }
        if (orderSetting.getNumber() <= orderSetting.getReservations()){
            throw new RuntimeException("预约日期已经约满");
        }
        //判断是否为会员
        Member memberTemp = memberDao.findByTelephone(orderDto.getTelephone());
        //不是会员则添加会员
        if (memberTemp != null){
            memberDao.addMember(member);
        }else {
            member =memberDao.getCondition(member);
        }
        // 不能重复预约（同一个用户在同一天预约了同一个套餐）,先查找预约信息

        Order orderTemp = orderDao.findByMemberIdAndOrderDateAndSetmealId(member.getId(), orderDto.getOrderDate(), orderDto.getSetmealId());
        if (orderTemp != null){
            throw new RuntimeException("不能重复预约");
        }
        //添加预约
        order.setMemberId(member.getId());
        orderDao.addOrder(order);
        //修改预约人数
        ordersettingDao.editReservationsByOrderDate(orderDto.getOrderDate());
    }
}
